package cn.tedu._05mvcboot01.Controller;

import cn.tedu._05mvcboot01.pojo.dto.WeichatDTO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Controller:将此类标识为一个控制器,用于接收用户请求;
 */

@Controller
public class UserController {
    /**
     * 1.RequestMapping:根据请求的URL地址的路径,匹配对应的控制器方法
     * 2.ResponseBody:允许控制器方法以返回值[return]的形式返回给客户端数据
     */
    @RequestMapping(value = "v1/user/login")
    @ResponseBody
    public String login(){
        return "<h1 style='color:red'>恭喜你,登录成功</h1>";
    }

    /**
     * 1.客户端传递给服务器数据
     * http:localhost:8080/v1/users/reg?username=daikechuan&password=123456
     * 2.服务端接受数据的方式1
     * 通过HttpServeletRequest参数接受
     */
    @RequestMapping(value = "v1/users/reg")
    @ResponseBody
    public String reg(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return username + "-" + password;
    }

    /**
     * 2.服务端接收数据的方式2:通过声明参数方式接受
     * http:localhost:8080/v1/users/changepwd?username=daikechuan&newpwd=123456
     */
    @RequestMapping(value = "/v1/users/changepwd")
    @ResponseBody
    public String ChangePwd(String username,String newpwd){
        return username+"-"+newpwd;
    }

    /**
     * 3.DTO类方式接受
     * 功能:
     * http:localhost:8080/v1/users/weiChat?name=微信号&pwd=密码
     */
    @RequestMapping(value = "/v1/users/weiChat")
    @ResponseBody
    public String weiChat(WeichatDTO weichatDTO){
        String name = weichatDTO.getName();
        String pwd = weichatDTO.getPwd();
        return name+":"+pwd;
    }
}
